<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
	<th:block th:include="include :: header('流程定义')"/>
	<th:block th:include="include :: jasny-bootstrap-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
	<div class="row">
		<div class="col-sm-12 search-collapse">
			<form id="formId">
				<div class="select-list">
					<ul>
						<li>
							<p>流程KEY：</p>
							<input name="key" type="text"/>
						</li>
						<li>
							<p>名称：</p>
							<input name="name" type="text"/>
						</li>
						<li>
							<p>所属分类：</p>
							<input name="category" type="text"/>
						</li>
						<!--<li class="select-time">
                            <label>部署时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                        </li>-->
						<li>
							<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
									class="fa fa-search"></i>&nbsp;搜索</a>
							<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
									class="fa fa-refresh"></i>&nbsp;重置</a>
						</li>
					</ul>
				</div>
			</form>
		</div>

		<div class="btn-group-sm" id="toolbar" role="group">
			<div class="fileinput fileinput-new" data-provides="fileinput" id="processDefinitionDiv"
				 style="margin-bottom: 0; margin-right: -3px;">
										<span class="btn btn-success btn-file" style="font-size: 12px;">
											<span><i class="fa fa-upload"></i> 部署流程定义</span>
											<input multiple name="processDefinition" type="file">
										</span>
				<span style="color: red;">查看流程图模糊时，可选择 .bpmn 和 .png 一同打成 zip 包部署</span>
				<span class="fileinput-filename"></span>
				<a class="close fileinput-exists" data-dismiss="fileinput" href="#" style="float: none">&times;</a>
			</div>
		</div>
		<div class="col-sm-12 select-table table-striped">
			<table id="bootstrap-table" style="word-break:break-all; word-wrap:break-all;"></table>
		</div>
	</div>
</div>

<div th:include="include :: footer"></div>
<th:block th:include="include :: jasny-bootstrap-js"/>
<script th:inline="javascript">
	var prefix = ctx + "definition";

	$(function () {
		var options = {
			url: prefix + "/list",
			removeUrl: prefix + "/remove",
			exportUrl: prefix + "/export",
			modalName: "流程定义",
			uniqueId: "deploymentId",
			columns: [{
				checkbox: true
			},
				{
					field: 'id',
					title: '流程ID'
				},
				{
					field: 'key',
					title: '流程KEY'
				},
				{
					field: 'name',
					title: '流程名称'
				},
				{
					field: 'version',
					title: '版本'
				},
				{
					field: 'description',
					title: '流程描述'
				},
				{
					field: 'category',
					title: '所属分类'
				},
				{
					field: 'deploymentTime',
					title: '部署时间'
				},
				{
					field: 'resourceName',
					align: 'center',
					title: '流程定义',
					formatter: function (value, row, index) {
						// var url = ctx + 'repository/process-definitions/' + row.id + '/image';
						var url = ctx + 'definition/readResource?pdid=' + row.id + '&resourceName=' + value;
						return '<a href="' + url + '" target="_blank"> ' + value + '</a>';
					}
				},
				{
					field: 'diagramResourceName',
					align: 'center',
					title: '流程图',
					formatter: function (value, row, index) {
						var url = ctx + 'definition/readResource?pdid=' + row.id + '&resourceName=' + value;
						return '<a href="' + url + '" target="_blank"> ' + row.key + '.png' + '</a>';
					}
				},
				{
					field: 'suspendStateName',
					align: 'center',
					title: '流程定义状态',
					formatter: function (value, row, index) {
						return '<span class="badge badge-primary">' + value + '</span>';
					}
				},
				{
					title: '操作',
					align: 'center',
					formatter: function (value, row, index) {
						var actions = [];
						var suspendOrActive = row.suspendState === '2' ? '激活' : '挂起';
						var icon = row.suspendState === '2' ? 'fa fa-check' : 'fa fa-stop';
						actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="suspendOrActiveApply(\'' + row.id + '\',\'' + row.suspendState + '\')"><i class="' + icon + '"></i> ' + suspendOrActive + '</a> ');
						actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="convert2Model(\'' + row.id + '\')"><i class="fa fa-exchange"></i> 转模型</a>');
						actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.deploymentId + '\')"><i class="fa fa-remove"></i> 删除</a>');
						return actions.join('');
					}
				}]
		};
		$.table.init(options);

		initProcessDefinitionFileInput();
	});

	function initProcessDefinitionFileInput() {
		$('#processDefinitionDiv').on('change.bs.fileinput', function (e) {
			// 处理自己的业务
			var formdata = new FormData();
			formdata.append("processDefinition", $('input[type=file]')[0].files[0]);
			$.ajax({
				url: prefix + '/upload',
				data: formdata,
				type: "post",
				processData: false,
				contentType: false,
				success: function (result) {
					$('#processDefinitionDiv').fileinput('reset');     // 重置
					$.operate.ajaxSuccess(result);
				}
			})
		});
	}

	function suspendOrActiveApply(id, suspendState) {
		var suspendOrActive = suspendState === '2' ? '激活' : '挂起';
		$.modal.confirm("确认要" + suspendOrActive + "流程定义吗?", function () {
			var url = ctx + "definition/suspendOrActiveApply";
			var data = {"id": id, "suspendState": suspendState};
			$.operate.submit(url, "post", "json", data);
		});
	}

	function convert2Model(id) {
		$.modal.confirm("确认要转换成流程模型吗?", function () {
			var url = ctx + "definition/convert2Model";
			var data = {"processDefinitionId": id};
			$.operate.submit(url, "post", "json", data);
		});
	}

</script>
</body>
</html>
